package com.kgc.scd.controller;

import com.kgc.scd.annotation.RequestPermission;
import com.kgc.scd.annotation.RequestUser;
import com.kgc.scd.service.IndexSearchService;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import com.kgc.scd.util.UserAnno;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: com.kgc.scd.controller
 * @author: HarryXzy
 * @create: 2022-06-16 10:04
 * @description: 首页搜索
 **/
@RestController
@Api(tags = "04、首页搜索操作入口")
public class IndexSearchController {

    @Autowired
    private IndexSearchService indexSearchService;

    @ApiOperation(value = "首页搜索", notes = "首页搜索商品")
    // 返回状态说明
    @ApiResponses({
            @ApiResponse(code = 500, message = "系统错误"),
            @ApiResponse(code = 200, message = "成功")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchKey", value = "搜索词"),
            @ApiImplicitParam(name = "pageNo", value = "页码"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数"),
            @ApiImplicitParam(name = "orderByPrice", value = "按照价格排序，0：降序 1：升序")
    })
    @PostMapping("/indexSearch")
    @RequestPermission
    public RequestResult<Object> indexSearch(@RequestUser UserAnno userAnno,
                                             @RequestParam(value = "searchKey",defaultValue = "BLMALL热卖")String searchKey,
                                             @RequestParam(value = "pageNo",defaultValue = "1")Integer pageNo,
                                             @RequestParam(value = "pageSize",defaultValue = "6")Integer pageSize,
                                             @RequestParam(value = "orderByPrice",defaultValue = "null")String orderByPriceDesc) throws IOException {
        Map<String, Object> result = indexSearchService.indexSearch(userAnno.getUserId(), searchKey, pageNo, pageSize, orderByPriceDesc);
        if (result.get("code").equals("200")) {
            return ResultBuildUtil.success(result.get("data"));
        }
        return ResultBuildUtil.fail(result.get("code").toString(), result.get("msg").toString());

    }

}
